import _ from 'lodash'

interface obj{
  [key:string]:any
}

const func = {
    /**
   *  将formTemplate处理为可配置的格式
   */
    formatDefaultOption(defaultOption:obj, data:obj) {
        const res:obj = {}
        const deepTransForm = (cur:obj, parentKey:string) => {
            Object.keys(cur).forEach(key => {
                cur[key].model = `${parentKey}.${key}`
                if (cur[key].tabs) {
                    this.formatSeries(cur[key], data, `${parentKey}.${key}`)
                } else if (cur[key].children) {
                    deepTransForm(cur[key].children, `${parentKey}.${key}`)
                }
            })
        }
        Object.keys(defaultOption).forEach(key => {
            res[key] = defaultOption[key]
            if (defaultOption[key].tabs) {
                this.formatSeries(defaultOption[key], data, key)
            } else if (defaultOption[key].children) {
                deepTransForm(defaultOption[key].children, key)
            }
        })
        return res
    },
    /**
     * 处理formTemplate中tabs格式
     */
    formatSeries(tabsTemplate:obj, data:obj, key:string) {
        const curData = data[tabsTemplate.model]
        const res:obj = {}
        const deepTransFormTabs = (cur:obj, index:number, parentKey:string) => {
            Object.keys(cur).forEach(curKey => {
                cur[curKey].model = `${parentKey}.${curKey}`
                if (cur[curKey].children) {
                    deepTransFormTabs(cur[curKey].children, index, `${parentKey}.${curKey}`)
                }
            })
            return cur
        }
        const templates = curData.map((item:obj, idx:number) => {
            const data = _.cloneDeep(tabsTemplate.children)
            return deepTransFormTabs(data, idx, `${tabsTemplate.model}[${idx}]`)
        })
        tabsTemplate.children = templates
        return templates
    },  
}
export default func